/**
 * Project: VelocityTest
 * 
 * File Created at 2012-9-22
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.biz.entity.autocode;

import java.util.Date;

import com.liangdev.common.util.DateUtil;
import com.liangdev.common.util.StringUtil;

/**
 * 字段Meta信息
 * 
 * @author chongan.wangca
 */
public class FieldMetaData {

    private String dbTableName;

    /* 数据库字段类型，比如：VARCHAR2 */
    private String dbFieldType;

    /* 数据库字段名称，全部为大写，比如：PHONE_NAME */
    private String dbFiledName;

    /* 字段备注 */
    private String comment;

    /* 值 */
    private Object value;

    public String getValueStr() {
        if (value == null) {
            return "<null>";
        }

        if (value instanceof Date) {
            return DateUtil.date2String((Date) value, null);
        }
        if (value instanceof String) {
            return (String) value;
        }
        if (value instanceof Long) {
            return String.valueOf(((Long) value));
        }
        if (value instanceof Integer) {
            return String.valueOf(((Integer) value));
        }
        return value.toString();
    }

    /**
     * 获取字段的JdbcType,用于ibatis的jdbcType定义时候
     * 
     * @return
     */
    public String getJdbcType() {

        String dbFieldType = this.dbFieldType.toUpperCase();
        if (dbFieldType.startsWith("VARCHAR")) {
            return "VARCHAR";
        }
        if (dbFieldType.startsWith("CHAR")) {
            return "CHAR";
        }
        if (dbFieldType.startsWith("CLOB")) {
            return "CLOB";
        }
        if (dbFieldType.startsWith("BLOB")) {
            return "BLOB";
        }
        if (dbFieldType.startsWith("BIGINT")) {
            return "BIGINT";
        }
        if (dbFieldType.startsWith("TINYINT")) {
            return "TINYINT";
        }
        if (dbFieldType.startsWith("NUMBER")) {
            return "NUMBER";
        }
        if (dbFieldType.startsWith("DATETIME")) {
            return "DATETIME";
        }
        if (dbFieldType.startsWith("TIMESTAMP")) {
            return "TIMESTAMP";
        }
        if (dbFieldType.startsWith("DATE")) {
            return "DATE";
        }
        if (dbFieldType.startsWith("INT")) {
            return "INT";
        }
        if (dbFieldType.startsWith("DECIMAL")) {
            return "DECIMAL";
        }
        if (dbFieldType.startsWith("DOUBLE")) {
            return "DOUBLE";
        }
        return null;
    }

    /**
     * 获取字段的java类型全路径
     * 
     * @return
     */
    public String getFieldTypeQualified() {

        String dbFieldType = this.dbFieldType.toUpperCase();
        if (dbFieldType.startsWith("VARCHAR") || dbFieldType.startsWith("CHAR")
                || dbFieldType.startsWith("CLOB") || dbFieldType.startsWith("BLOB")) {
            return "java.lang.String";
        } else if (dbFieldType.startsWith("BIGINT") || dbFieldType.startsWith("NUMBER")) {
            return "java.lang.Long";
        } else if (dbFieldType.startsWith("DATE")) {
            return "java.util.Date";
        } else if (dbFieldType.startsWith("INT") || dbFieldType.startsWith("TINYINT")) {
            return "java.lang.Integer";
        } else if (dbFieldType.startsWith("DECIMAL") || dbFieldType.startsWith("DOUBLE")) {
            return "java.lang.Double";
        }

        return null;
    }

    /**
     * 获取字段的java类型
     * 
     * @return
     */
    public String getFieldType() {

        String dbFieldType = this.dbFieldType.toUpperCase();
        if (dbFieldType.startsWith("VARCHAR") || dbFieldType.startsWith("CHAR")
                || dbFieldType.startsWith("CLOB") || dbFieldType.startsWith("BLOB")) {
            return "String";
        } else if (dbFieldType.startsWith("BIGINT") || dbFieldType.startsWith("NUMBER")) {
            return "Long";
        } else if (dbFieldType.startsWith("DATE")) {
            return "Date";
        } else if (dbFieldType.startsWith("INT") || dbFieldType.startsWith("TINYINT")) {
            return "Integer";
        } else if (dbFieldType.startsWith("DECIMAL") || dbFieldType.startsWith("DOUBLE")) {
            return "Double";
        }

        return null;
    }

    public String getDbFieldType() {
        return dbFieldType;
    }

    public void setDbFieldType(String dbFieldType) {
        this.dbFieldType = dbFieldType;
    }

    /**
     * 字段名称，会自动把下横线_转化为大写，即驼峰形式，并且首字母小写。比如：phoneNumber
     * 
     * @return
     */
    public String getFieldNameLF() {
        String newFieldName = StringUtil.firstUpperCaseAll(dbFiledName.toLowerCase(), "_");
        newFieldName = StringUtil.firstLetterLower(newFieldName);
        return newFieldName;
    }

    /**
     * 字段名称，会自动把下横线_转化为大写，即驼峰形式，并且首字母大写。比如：PhoneNumber
     * 
     * @return
     */
    public String getFieldNameUF() {
        return StringUtil.firstLetterUpper(getFieldNameLF());
    }

    public void setDbFiledName(String dbFiledName) {
        this.dbFiledName = dbFiledName;
    }

    /**
     * 数据库字段名称，全部为大写，比如：PHONE_NAME
     * 
     * @return
     */
    public String getDbFieldNameUC() {
        return this.dbFiledName.toUpperCase();
    }

    /**
     * 数据库字段名称，全部为小写，比如：phone_name
     * 
     * @return
     */
    public String getDbFieldNameLC() {
        return this.dbFiledName.toLowerCase();
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    /**
     * @return the dbTableName
     */
    public String getDbTableNameUC() {
        return dbTableName.toUpperCase();
    }

    /**
     * @param dbTableName the dbTableName to set
     */
    public void setDbTableName(String dbTableName) {
        this.dbTableName = dbTableName;
    }

}
